package com.phamkhanh.controller;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.phamkhanh.bo.GpsBO;
import com.phamkhanh.bo.UserBO;
import com.phamkhanh.domain.Gps;
import com.phamkhanh.domain.User;
import com.phamkhanh.model.GpsInfo;

@Controller
public class GpsController {
	
	private Logger logger = Logger.getLogger(GpsController.class);
	
	private GpsBO gpsBO;
	private UserBO userBO;
	
	@Autowired
	public GpsController(GpsBO gpsBO, UserBO userBO) {
		this.gpsBO = gpsBO;
		this.userBO = userBO;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
	}
	
	@RequestMapping(value="/sendgps.html")
	@ResponseBody
	public String doPost(@Valid @ModelAttribute("gpsInfo") GpsInfo gpsInfo,
			BindingResult result){
		logger.info(gpsInfo);
		if(!result.hasErrors()){
			logger.info("Thông tin gps post đúng định dạng");
			User user = userBO.findByUserNamePassword(gpsInfo.getUserName(), gpsInfo.getPassword());
			if(user != null){
				logger.info("user gửi thông tin gps : "+user);
				Gps gps = new Gps();
				gps.setLongitude(gpsInfo.getLongitude());
				gps.setLatitude(gpsInfo.getLatitude());
				gps.setDateUpdate(gpsInfo.getDateUpdate());
				gpsBO.save(gps, user);
				logger.info("Lưu gps thành công");
				return "Success"; 
			}else {
				logger.info("Không xác định user gửi thông tin");
				return "UserNotDefine";
			}
		}
		logger.info("error count: "+result.getErrorCount()+result.getAllErrors());
		logger.info("Thông tin post không đúng");
		return "GpsInfoNotCorrect";	
	}
	
	
	@RequestMapping("/view-map.html")
	public String viewMapAction(ModelMap model, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute("user");
		if(user == null){
			logger.info("user chưa đăng nhập");
			return "redirect:login.html";
		}
		else{
			logger.info("user đang đăng nhập:"+user);
			List<Gps> listGps = gpsBO.findGpsByUser(user);
			Collections.reverse(listGps);
			model.addAttribute("listGps", listGps);
			logger.info(listGps);
			return "view-map.jsp";
		}
	}
	
}
